package com.trussell.genesis.dialogs;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.WindowConstants;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;

import com.trussell.genesis.GenesisImplementor;

public class ChangeWorldSizeDialog extends JDialog {

	JSpinner spinner;
	JButton cancelButton;
	JButton okButton;
	
	public ChangeWorldSizeDialog() {
		super();
		setTitle("Change World Size");
		setModal(true);

		initComponents();
		
		okButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				GenesisImplementor.getInstance().setMapSize((Integer)(spinner.getValue()));
				dispose();
			}
		});
		
		cancelButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		
		setLocationRelativeTo(null);
		setVisible(true);
	}
		
	private void initComponents() {
		JLabel jLabel1 = new JLabel();
		spinner = new JSpinner();
		cancelButton = new JButton();
		okButton = new JButton();

		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

		jLabel1.setText("New World Size:");

		spinner.setModel(new SpinnerNumberModel(1, 1, 12, 1));
		spinner.setName("jSpinner1");

		cancelButton.setText("Cancel"); 
		cancelButton.setName("jButton2"); 

		okButton.setText("Okay");
		okButton.setName("jButton1"); 

		GroupLayout layout = new GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup().addContainerGap()
								.addComponent(jLabel1).addPreferredGap(
										ComponentPlacement.RELATED)
								.addComponent(spinner,
										GroupLayout.PREFERRED_SIZE,
										GroupLayout.DEFAULT_SIZE,
										GroupLayout.PREFERRED_SIZE)
								.addContainerGap(119, Short.MAX_VALUE))
				.addGroup(
						Alignment.TRAILING,
						layout.createSequentialGroup().addContainerGap(108,
								Short.MAX_VALUE).addComponent(cancelButton)
								.addPreferredGap(ComponentPlacement.RELATED)
								.addComponent(okButton).addContainerGap()));
		layout
				.setVerticalGroup(layout
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								layout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												layout
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(jLabel1)
														.addComponent(
																spinner,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE))
										.addGap(11, 11, 11)
										.addGroup(
												layout.createParallelGroup(
														Alignment.BASELINE)
														.addComponent(okButton)
														.addComponent(cancelButton))
										.addContainerGap(
												GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)));

		pack();
	}
}
